{extend name="../public/header" /}
{block name="body"}
<body>
{include file="../public/top" uname="$roleInfo.uname" lv="$roleInfo.ulv" ls="$roleInfo.uyxb" jp="$roleInfo.uczb" /}
<div class="subject main">
    <div class="layui-row">
        <div class="content">
            <div class="current">
                <span class="position">{$roleInfo.map.mname}</span>
                <span class="state">{if $roleInfo.map.ispvp==0}[安全区]{else/}[PVP]{/if}</span>
                <span class="info ajax-goto" data-url="{:url('index')}">返回</span>
            </div>
            <div class="npc">
                <div class="layui-card">
                    <div class="layui-card-header">[Lv:{$info.nlv}] {$info.nname} <span class="nickname">{if $info.nickname!=''}<{$info.nickname}>{/if}</span></div>
                    <div class="layui-card-body">{$info.ninfo|default=''}</div>
                </div>
                <div class="layui-panel">
                    <div style="padding:10px;">
                        <div class="layui-row layui-col-space10 shop">
                            {volist name="info['list']" id="vo"}
                            <div class="layui-col-xs4">
                                <div class="grid-demo">
                                    <div class="shop-title">{$vo.ypname}</div>
                                    <div class="shop-des">[lv:{$vo.yplv}]
                                        {if $vo.type == '恢复类'}
                                        {if $vo.yphp > 0}hp:+{$vo.yphp}{/if}
                                        {if $vo.ypmana > 0}mp:+{$vo.ypmana}{/if}
                                        {/if}
                                        {if $vo.type == '修为类'}exp:+{$vo.exp}{/if}
                                        {if $vo.type == '增益类'}{/if}
                                    </div>
                                    <div class="shop-lable">售:{$vo.ypjg} <img class="buy-shop" data-value="{$vo.ypid}" data-price="{$vo.ypjg}" src="/public/index/image/add-cart.png" style="float:right;height:30px;"></div>
                                </div>
                            </div>
                            {/volist}
                        </div>
                    </div>
                </div>
            </div>
            <div class="labels">
                <div class="layui-panel" style="padding:10px;">
                    <input type="hidden" id="npc_id" name="npc_id" value="{$info.id|default='0'}">
                    <input type="hidden" id="shop_id" name="shop_id" value="0">
                    <input type="hidden" id="cart" name="cart" value="0">
                    <i class="layui-icon layui-icon-cart"> ：0</i>
                    <i class="layui-icon layui-icon-diamond"> ：0</i>
                    <a class="layui-btn layui-btn-xs layui-btn-primary layui-border-blue ajax-buy" style="position: absolute;right: 10px;top: 15px;">采购</a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
{/block}
{block name="script"}
<script>
    layui.use(['jquery', 'form', 'layajax', 'upload', 'flow', 'element'], function () {
        var $ = layui.jquery
            , layajax = layui.layajax
            , upload = layui.upload
            , element = layui.element
            , flow = layui.flow
            , form = layui.form;

        $('.buy-shop').on('click',function(){
            var obj = $(this);
            var ypid = obj.data('value');
            var price = obj.data('price');
            var shop_id = $('#shop_id').val();

            console.log('ypid',ypid,'price',price,'shop_id',shop_id);

            var cart = 0;
            var diamond = 0;

            if(ypid == shop_id){
                cart = Number($('#cart').val()) + 1;
                diamond = cart * price;
            }else{
                cart = 1;
                diamond = price;
            }

            $('.layui-icon-cart').html(' ：' + cart);
            $('.layui-icon-diamond').html(' ：' + diamond);

            $('#shop_id').val(ypid);
            $('#cart').val(cart);
        });
        
        $('.ajax-buy').on('click',function () {
            var npc_id = $('#npc_id').val();
            var shop_id = $('#shop_id').val();
            var cart = $('#cart').val();

            if(shop_id == 0){
                layer.msg('请选择商品');
                return false;
            }

            console.log('shop_id',shop_id,'cart',cart);

            var map = {};
            map['npc_id'] = npc_id;
            map['shop_id'] = shop_id;
            map['cart'] = cart;

            $.post("{:url('home/buyDrug')}", map, function (res) {
                if (res.code == undefined) {
                    console.log('res为json字符串', res);
                    res = JSON.parse(res);
                } else {
                    console.log('res为json对象', res);
                }
                if (res.code == 1) {
                    layer.msg(res.msg,{icon:1,time:2000}, function(index){
                        if(res.data.url!='' && res.data.url!=undefined){
                            window.location.href = res.data.url;
                        }else{
                            location.reload();
                        }
                    });
                } else {
                    layer.msg(res.msg);
                }
            });

        });

        form.render();
    });
</script>
{/block}